package com.biconn.bigdata.es.client.config;/**
 * Created by Administrator on 2018/8/4.
 */

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.biconn.bigdata.es.client.config.annontation.IndexFileAnnontation;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * botu
 * com.bicon.botu.util
 *
 * @author 123774135@qq.com
 * @create 2018-08-04 21:23
 **/
public class BeanPropertyResolve {


    private static Logger LOGGER = LoggerFactory.getLogger(BeanPropertyResolve.class);

    
    private static final ConcurrentHashMap<String,Map<String,String>> map = new ConcurrentHashMap<String,Map<String,String>>();

    public static Field getField(Class<?> clazz,String fieldName) throws NoSuchFieldException {
        return clazz.getDeclaredField(fieldName);
    }

    
    
    /**
     * 构建搜索索引模型
     * @param t
     * @param <T>
     * @return
     */
    public static <T> List<ElasticsearchMapping> builderElasticsearchMapping(T t){
        List<ElasticsearchMapping> list = Lists.newArrayList();
        PropertyDescriptor [] propertyDescriptors = BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(t);
        for(PropertyDescriptor p : propertyDescriptors){
            ElasticsearchMapping elasticsearchMapping = new ElasticsearchMapping();
            if("class".equals(p.getName())){
                continue;
            }
            IndexFileAnnontation indexFileAnnontation = null;
            try {

                Field field = t.getClass().getDeclaredField(p.getName());
                field.setAccessible(true);
                indexFileAnnontation = field.getAnnotation(IndexFileAnnontation.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(null != indexFileAnnontation){
                //xcontentBuilder.startObject(indexFileAnnontation.name()).field("type",indexFileAnnontation.type()).field("index",indexFileAnnontation.index().name().toLowerCase());
               // LOGGER.info(indexFileAnnontation.name() +"------"+indexFileAnnontation.type());
                elasticsearchMapping.setIndex(indexFileAnnontation.index());
                elasticsearchMapping.setName(indexFileAnnontation.name());
                elasticsearchMapping.setStore(indexFileAnnontation.store());
                elasticsearchMapping.setType(indexFileAnnontation.type());
                elasticsearchMapping.setAnalyzer(indexFileAnnontation.analyzer());
                elasticsearchMapping.setSearchAnalyzer(indexFileAnnontation.search_analyzer());
                elasticsearchMapping.setSimilarity(indexFileAnnontation.similarity());
                list.add(elasticsearchMapping);
            }
        }
        return list;
    }
    
    
    /**
     * 
     * @Title: builderFileAndAnnontationMpper   
     * @Description: 建立字段和注解的对于关系   
     * @return      
     * Map<String,Map<String,String>>      
     * @throws
     */
    public static <T> Map<String,String> builderFileAndAnnontationMpper(T t){
    	String key = t.getClass().getName();
    	if(!map.containsKey(key)) {
    		PropertyDescriptor [] propertyDescriptors = BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(t);
            Map<String,String> realtionMap = Maps.newHashMap();
            for(PropertyDescriptor p : propertyDescriptors){
                if("class".equals(p.getName())){
                    continue;
                }
                IndexFileAnnontation indexFileAnnontation = null;
                try {

                    Field field = t.getClass().getDeclaredField(p.getName());
                    field.setAccessible(true);
                    indexFileAnnontation = field.getAnnotation(IndexFileAnnontation.class);
                    if(null != indexFileAnnontation){
                    	realtionMap.put(indexFileAnnontation.name(),field.getName());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            map.put(key, realtionMap);
    	}
        
        return map.get(key);
    }

    
}
